/*!
 * \file test_wts_utils_comprehensive.cpp
 * \brief WTS工具函数综合测试
 * 
 * \details 该文件包含WTS工具函数的全面测试用例，
 *          涵盖字符串处理、时间处理、数据转换等功能
 * 
 * \author AI Assistant
 * \date 2024
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "../../src/Includes/WTSObject.hpp"
#include "../../src/WTSUtils/WTSCmpHelper.hpp"
#include "../../src/WTSUtils/WtLMDB.hpp"
#include <memory>
#include <vector>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace wtp;

/*!
 * \brief WTS工具函数综合测试类
 */
class WTSUtilsComprehensiveTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 测试前准备
    }
    
    void TearDown() override {
        // 测试后清理
    }
};

/*!
 * \brief 测试字符串工具函数
 * 
 * \details 验证字符串处理功能：
 *          - 字符串分割
 *          - 字符串替换
 *          - 字符串格式化
 *          - 编码转换
 */
TEST_F(WTSUtilsComprehensiveTest, TestStringUtilities) {
    // 测试字符串基本操作
    std::string test_str = "Hello,World,Test";
    
    // 验证字符串不为空
    EXPECT_FALSE(test_str.empty());
    EXPECT_GT(test_str.length(), 0);
    
    // 测试字符串查找
    size_t pos = test_str.find(",");
    EXPECT_NE(pos, std::string::npos);
    EXPECT_EQ(pos, 5);
    
    // 测试子字符串
    std::string sub = test_str.substr(0, 5);
    EXPECT_EQ(sub, "Hello");
    
    // 测试字符串替换
    std::string replaced = test_str;
    size_t replace_pos = replaced.find(",");
    if (replace_pos != std::string::npos) {
        replaced.replace(replace_pos, 1, "|");
    }
    EXPECT_EQ(replaced, "Hello|World,Test");
    
    std::cout << "字符串工具测试通过" << std::endl;
}

/*!
 * \brief 测试时间工具函数
 * 
 * \details 验证时间处理功能：
 *          - 时间戳转换
 *          - 时间格式化
 *          - 时间计算
 *          - 时区处理
 */
TEST_F(WTSUtilsComprehensiveTest, TestTimeUtilities) {
    // 获取当前时间
    auto now = std::chrono::system_clock::now();
    auto time_t_now = std::chrono::system_clock::to_time_t(now);
    
    // 验证时间戳有效性
    EXPECT_GT(time_t_now, 0);
    
    // 测试时间转换
    auto duration = now.time_since_epoch();
    auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
    EXPECT_GT(millis, 0);
    
    // 测试时间计算
    auto future = now + std::chrono::hours(1);
    auto past = now - std::chrono::hours(1);
    
    EXPECT_GT(future, now);
    EXPECT_LT(past, now);
    
    // 测试时间差计算
    auto diff = std::chrono::duration_cast<std::chrono::seconds>(future - past);
    EXPECT_EQ(diff.count(), 7200); // 2小时 = 7200秒
    
    std::cout << "时间工具测试通过，当前时间戳: " << time_t_now << std::endl;
}

/*!
 * \brief 测试数据转换工具函数
 * 
 * \details 验证数据转换功能：
 *          - 数值转换
 *          - 字符串转数值
 *          - 进制转换
 *          - 精度处理
 */
TEST_F(WTSUtilsComprehensiveTest, TestDataConversionUtilities) {
    // 测试整数转换
    int int_val = 12345;
    std::string int_str = std::to_string(int_val);
    EXPECT_EQ(int_str, "12345");
    
    // 测试字符串转整数
    int converted_int = std::stoi(int_str);
    EXPECT_EQ(converted_int, int_val);
    
    // 测试浮点数转换
    double double_val = 123.456789;
    std::string double_str = std::to_string(double_val);
    EXPECT_FALSE(double_str.empty());
    
    // 测试字符串转浮点数
    double converted_double = std::stod(double_str);
    EXPECT_NEAR(converted_double, double_val, 0.000001);
    
    // 测试边界值
    EXPECT_NO_THROW({
        std::string max_int_str = std::to_string(INT_MAX);
        int max_int = std::stoi(max_int_str);
        EXPECT_EQ(max_int, INT_MAX);
    });
    
    // 测试科学计数法
    double scientific = 1.23e-4;
    std::string sci_str = std::to_string(scientific);
    EXPECT_FALSE(sci_str.empty());
    
    std::cout << "数据转换测试通过" << std::endl;
    std::cout << "整数: " << int_val << " -> " << int_str << " -> " << converted_int << std::endl;
    std::cout << "浮点数: " << double_val << " -> " << converted_double << std::endl;
}

/*!
 * \brief 测试文件路径工具函数
 * 
 * \details 验证文件路径处理功能：
 *          - 路径分割
 *          - 路径组合
 *          - 扩展名处理
 *          - 路径标准化
 */
TEST_F(WTSUtilsComprehensiveTest, TestFilePathUtilities) {
    // 测试路径处理
    std::string full_path = "/home/user/documents/test.txt";
    
    // 查找最后一个路径分隔符
    size_t last_slash = full_path.find_last_of("/\\");
    EXPECT_NE(last_slash, std::string::npos);
    
    // 提取目录路径
    std::string dir_path = full_path.substr(0, last_slash);
    EXPECT_EQ(dir_path, "/home/user/documents");
    
    // 提取文件名
    std::string filename = full_path.substr(last_slash + 1);
    EXPECT_EQ(filename, "test.txt");
    
    // 提取扩展名
    size_t last_dot = filename.find_last_of(".");
    if (last_dot != std::string::npos) {
        std::string extension = filename.substr(last_dot + 1);
        EXPECT_EQ(extension, "txt");
        
        std::string basename = filename.substr(0, last_dot);
        EXPECT_EQ(basename, "test");
    }
    
    // 测试Windows路径
    std::string win_path = "C:\\Users\\Test\\file.dat";
    size_t win_slash = win_path.find_last_of("/\\");
    EXPECT_NE(win_slash, std::string::npos);
    
    std::cout << "文件路径工具测试通过" << std::endl;
    std::cout << "完整路径: " << full_path << std::endl;
    std::cout << "目录: " << dir_path << std::endl;
    std::cout << "文件名: " << filename << std::endl;
}

/*!
 * \brief 测试内存工具函数
 * 
 * \details 验证内存处理功能：
 *          - 内存分配
 *          - 内存拷贝
 *          - 内存比较
 *          - 内存清零
 */
TEST_F(WTSUtilsComprehensiveTest, TestMemoryUtilities) {
    const size_t buffer_size = 1024;
    
    // 测试内存分配
    char* buffer1 = new char[buffer_size];
    char* buffer2 = new char[buffer_size];
    
    ASSERT_NE(buffer1, nullptr);
    ASSERT_NE(buffer2, nullptr);
    
    // 测试内存清零
    memset(buffer1, 0, buffer_size);
    memset(buffer2, 0, buffer_size);
    
    // 验证内存已清零
    for (size_t i = 0; i < buffer_size; ++i) {
        EXPECT_EQ(buffer1[i], 0);
        EXPECT_EQ(buffer2[i], 0);
    }
    
    // 测试内存设置
    memset(buffer1, 0xAA, buffer_size);
    for (size_t i = 0; i < buffer_size; ++i) {
        EXPECT_EQ(static_cast<unsigned char>(buffer1[i]), 0xAA);
    }
    
    // 测试内存拷贝
    memcpy(buffer2, buffer1, buffer_size);
    
    // 验证拷贝结果
    int cmp_result = memcmp(buffer1, buffer2, buffer_size);
    EXPECT_EQ(cmp_result, 0);
    
    // 测试部分内存修改
    buffer2[100] = 0x55;
    cmp_result = memcmp(buffer1, buffer2, buffer_size);
    EXPECT_NE(cmp_result, 0);
    
    // 但前100字节应该相同
    cmp_result = memcmp(buffer1, buffer2, 100);
    EXPECT_EQ(cmp_result, 0);
    
    // 清理内存
    delete[] buffer1;
    delete[] buffer2;
    
    std::cout << "内存工具测试通过，缓冲区大小: " << buffer_size << " 字节" << std::endl;
}

/*!
 * \brief 测试数学工具函数
 * 
 * \details 验证数学计算功能：
 *          - 基本运算
 *          - 精度比较
 *          - 范围检查
 *          - 统计计算
 */
TEST_F(WTSUtilsComprehensiveTest, TestMathUtilities) {
    // 测试基本数学运算
    double a = 10.5;
    double b = 3.2;
    
    double sum = a + b;
    double diff = a - b;
    double product = a * b;
    double quotient = a / b;
    
    EXPECT_NEAR(sum, 13.7, 0.0001);
    EXPECT_NEAR(diff, 7.3, 0.0001);
    EXPECT_NEAR(product, 33.6, 0.0001);
    EXPECT_NEAR(quotient, 3.28125, 0.0001);
    
    // 测试浮点数比较
    double epsilon = 1e-9;
    double val1 = 1.0 / 3.0;
    double val2 = 0.333333333;
    
    bool is_equal = std::abs(val1 - val2) < epsilon;
    EXPECT_TRUE(is_equal);
    
    // 测试范围检查
    double value = 5.5;
    double min_val = 1.0;
    double max_val = 10.0;
    
    bool in_range = (value >= min_val) && (value <= max_val);
    EXPECT_TRUE(in_range);
    
    // 测试边界值
    EXPECT_TRUE((min_val >= min_val) && (min_val <= max_val));
    EXPECT_TRUE((max_val >= min_val) && (max_val <= max_val));
    
    // 测试数组统计
    std::vector<double> data = {1.0, 2.0, 3.0, 4.0, 5.0};
    
    double total = 0.0;
    for (double val : data) {
        total += val;
    }
    double average = total / data.size();
    
    EXPECT_NEAR(total, 15.0, 0.0001);
    EXPECT_NEAR(average, 3.0, 0.0001);
    
    // 找最大值和最小值
    double min_element = *std::min_element(data.begin(), data.end());
    double max_element = *std::max_element(data.begin(), data.end());
    
    EXPECT_EQ(min_element, 1.0);
    EXPECT_EQ(max_element, 5.0);
    
    std::cout << "数学工具测试通过" << std::endl;
    std::cout << "基本运算: " << a << " + " << b << " = " << sum << std::endl;
    std::cout << "数组统计: 总和=" << total << ", 平均值=" << average << std::endl;
}

/*!
 * \brief 测试工具函数的完整工作流程
 * 
 * \details 验证工具函数的综合使用：
 *          - 多种工具函数组合使用
 *          - 实际应用场景模拟
 *          - 错误处理验证
 */
TEST_F(WTSUtilsComprehensiveTest, TestUtilitiesCompleteWorkflow) {
    // 模拟一个完整的数据处理流程
    
    // 1. 字符串处理
    std::string input_data = "SHFE.rb2501,3850.5,1000,20241201,093000";
    
    // 2. 数据解析
    std::vector<std::string> fields;
    size_t start = 0;
    size_t pos = 0;
    
    while ((pos = input_data.find(',', start)) != std::string::npos) {
        fields.push_back(input_data.substr(start, pos - start));
        start = pos + 1;
    }
    fields.push_back(input_data.substr(start)); // 最后一个字段
    
    // 3. 验证解析结果
    ASSERT_EQ(fields.size(), 5);
    EXPECT_EQ(fields[0], "SHFE.rb2501");
    EXPECT_EQ(fields[1], "3850.5");
    EXPECT_EQ(fields[2], "1000");
    EXPECT_EQ(fields[3], "20241201");
    EXPECT_EQ(fields[4], "093000");
    
    // 4. 数据转换
    std::string contract = fields[0];
    double price = std::stod(fields[1]);
    int volume = std::stoi(fields[2]);
    int date = std::stoi(fields[3]);
    int time = std::stoi(fields[4]);
    
    // 5. 验证转换结果
    EXPECT_EQ(contract, "SHFE.rb2501");
    EXPECT_NEAR(price, 3850.5, 0.0001);
    EXPECT_EQ(volume, 1000);
    EXPECT_EQ(date, 20241201);
    EXPECT_EQ(time, 93000);
    
    // 6. 数据验证
    EXPECT_FALSE(contract.empty());
    EXPECT_GT(price, 0);
    EXPECT_GT(volume, 0);
    EXPECT_GT(date, 20000101); // 合理的日期范围
    EXPECT_LT(date, 30000101);
    EXPECT_GE(time, 0);
    EXPECT_LT(time, 240000); // 24小时制时间
    
    // 7. 格式化输出
    std::string formatted = contract + ": 价格=" + std::to_string(price) + 
                           ", 成交量=" + std::to_string(volume);
    
    EXPECT_FALSE(formatted.empty());
    EXPECT_NE(formatted.find(contract), std::string::npos);
    
    std::cout << "工具函数完整工作流程测试通过" << std::endl;
    std::cout << "原始数据: " << input_data << std::endl;
    std::cout << "解析字段数: " << fields.size() << std::endl;
    std::cout << "格式化结果: " << formatted << std::endl;
} 